// Package cmd @author <chengjiang@buffalo-robot.com>
// @date 2023/1/17
// @note
package cmd

import (
	"fmt"
	"github.com/blinkbean/dingtalk"
	"github.com/spf13/cobra"
)

const dingToken = "02eb59d8802f1288783c26e6f890fd6c8e511b12edd150a8dc080e9c4d100f86"

// notify represents the base command when called without any subcommands
var notify = &cobra.Command{
	Use:   "notify",
	Short: "notify, with the DingTalk Robot, notify users the message",
	Long: `notify, 
used to with the DingTalk Robot, notify users the message.
	notify：
		notify: with the DingTalk Robot, notify users the message.`,
	// Uncomment the following line if your bare application
	// has an action associated with it:
	Run: func(cmd *cobra.Command, args []string) {
		println("通知消息到钉钉，robot loading...")
		err := robotWorking()
		if err != nil {
			println("Oops wrong here as follows...")
			handleError(err)
		}
		println("Congratulations ！ Successfully running.")
	},
}

func init() {
	//// post url:
	//Conf.posturl.base = "8.129.20.12"
	//Conf.posturl.suffix = "/basis/deployment-file"
	//Conf.posturl.method = "post"
	//// oss:
	//Conf.oss.accessKeyID = "LTAI4GJAtq5WBvytduM5FJr8"
	//Conf.oss.accessKeySecret = "6hzU5i4k9NkdFV4fAGQbWfXU1L3Kbw"
	//Conf.oss.ossEndpoint = "oss-cn-chengdu.aliyuncs.com"
	//Conf.oss.bucketName = "bris4-app-publisher"
	// notify: by the DingTalk robot.
	Conf.dingTalk.dingTalkKey = dingToken
	Conf.dingTalk.dingType = "markdown"
	//Conf.dingTalk.msg = []string{
	//	"drone ->",
	//	"---",
	//	"- Repo: ",
	//	"- Branch: ",
	//	"- Project: ",
	//	"- File: ",
	//	"- Url: ",
	//	"---",
	//	"> ding notify",
	//}

}

func robotWorking() error {
	ding := dingtalk.InitDingTalk([]string{dingToken}, "drone")
	msg := []string{
		"drone ->\n",
		"--- ",
		toString("- Repository: %s", repo),
		toString("- Project: %s", project),
		toString("- Branch: %s", branch),
		toString("- post: <http://8.129.20.12/%s>", Conf.posturl.suffix),
		toString("- oss: <https://bris4-app-publisher.oss-cn-chengdu.aliyuncs.com/%s>", objname),
		"--- ",
		"> ding notify",
	}
	fmt.Println(msg)
	err := ding.SendMarkDownMessageBySlice("Come on! Notifying...", msg)
	return err
}

func toString(tmp string, info string) string {
	return fmt.Sprintf(tmp, info)
}
